<template>
  <div class="rightbox">
    <h1>医院介绍</h1>
    <div class="add-box" style="width:430px;">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item
          label="医院名称"
          prop="name"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="医院地址"
          prop="address"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item
          label="咨询热线"
          prop="phone"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item
          label="医院级别"
          prop="levelinfo"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.levelinfo"></el-input>
        </el-form-item>
        <el-form-item
          label="取号地址"
          prop="taskplan"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.taskplan"></el-input>
        </el-form-item>
        <el-form-item
          label="医院简介"
          prop="remarks"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input type="textarea" v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-if="form.state == 1" @click="submit('form')">提交</el-button>
          <el-button type="primary" v-if="form.state == 2" @click="submit2('form')">修改提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
  data () {
    return {
      form: {
        name: '',
        address: '',
        phone: '',
        levelinfo: '',
        remarks: '',
        taskplan: '',
        state: ''
      }
    }
  },
  methods: {
    getList: function () {
      var that = this
      that.$pub.open()
      axios({
        method: 'post',
        url: '/gp/About/Index'
      })
        .then((res) => {
          that.$pub.close()
          if (res.data.code === 0) {
            that.form.address = res.data.data.address
            that.form.phone = res.data.data.phone
            that.form.name = res.data.data.name
            that.form.levelinfo = res.data.data.levelinfo
            that.form.remarks = res.data.data.remarks
            that.form.taskplan = res.data.data.taskplan
            if (res.data.data.name === null) {
              that.form.state = 1
            } else {
              that.form.state = 2
            }
          } else {
            that.$pub.failcode(that, res.data.code, res.data.data)
          }
        })
        .catch(function () {
          that.$pub.catchbox(that)
        })
    },
    submit: function (form) {
      var that = this
      that.$pub.open()
      that.$refs[form].validate((valid) => {
        if (valid) {
          axios.post('/gp/About/AddPost', qs.stringify({
            name: that.form.name,
            levelinfo: that.form.levelinfo,
            address: that.form.address,
            phone: that.form.phone,
            remarks: that.form.remarks,
            taskplan: that.form.taskplan
          })
          )
            .then((res) => {
              that.$pub.close()
              if (res.data.code === 0) {
                that.$pub.messageTwo(res.data.data, '提示')
                that.getList()
                that.form.state = 2
              } else {
                that.$pub.failcode(that, res.data.code, res.data.data)
              }
            })
            .catch(function () {
              that.$pub.catchbox(that)
            })
        }
      })
    },
    submit2: function (form) {
      var that = this
      that.$pub.open()
      that.$refs[form].validate((valid) => {
        if (valid) {
          axios.post('/gp/About/EidtPost', qs.stringify({
            name: that.form.name,
            levelinfo: that.form.levelinfo,
            address: that.form.address,
            phone: that.form.phone,
            remarks: that.form.remarks,
            taskplan: that.form.taskplan
          })
          )
            .then((res) => {
              that.$pub.close()
              if (res.data.code === 0) {
                that.$pub.messageTwo(res.data.data, '提示')
                that.getList()
                that.form.state = 2
              } else {
                that.$pub.failcode(that, res.data.code, res.data.data)
              }
            })
            .catch(function () {
              that.$pub.catchbox(that)
            })
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
